
$(document).ready(function() {
	getAllRoles();
	validateRule();
});


function getAllRoles(){

	var url = window.API_SERVER_URL + 'sys/role/list';

	$.ajax({
		cache : true,
		type : "GET",
		url : url,
		data : {},// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(roles) {
			if(roles && !roles.code){
				$.each(roles,function(index,role){
					$('#roles_div').append('<input id="'+role.roleId+'_ck" name="roleIds" type="checkbox" value="'+role.roleId+'" />&nbsp;');
					$('#roles_div').append('<label for="'+role.roleId+'_ck">'+role.roleName+'</label>&nbsp;&nbsp;&nbsp;&nbsp;');
				});
			}
		}
	});

}

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
var token = JSON.parse(localStorage.getItem("token"));
function getCheckedRoles() {
	var adIds = "";
	$("input:checkbox[name=role]:checked").each(function(i) {
		if (0 == i) {
			adIds = $(this).val();
		} else {
			adIds += ("," + $(this).val());
		}
	});
	return adIds;
}
function save() {
	$.ajax({
		cache : true,
		type : "POST",
		headers:window.PRIVATE_HEADERS,
		url : window.API_SERVER_URL + "sys/user/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i>";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			},
			username : {
				required : true,
				minlength : 2,
				remote : {
					headers:window.PRIVATE_HEADERS,
					url : ip + "sys/user/exitByUsername", // 后台处理程序
					type : "post", // 数据发送方式
					dataType : "json", // 接受数据格式
					data : { // 要传递的数据
						username : function() {
							return $("#username").val();
						}
					}
				}
			},
			password : {
				required : true,
				minlength : 6
			},
			confirm_password : {
				required : true,
				minlength : 6,
				equalTo : "#password"
			},
			deptName : {
				required : true
			},
			email : {
				required : true,
				email : true
			},
			mobile : {
            		required : true
            },
			topic : {
				required : "#newsletter:checked",
				minlength : 2
			},
			agree : "required"
		},
		messages : {

			name : {
				required : icon + "请输入姓名"
			},
			username : {
				required : icon + "请输入用户名",
				minlength : icon + "用户名必须两个字符以上",
				remote : icon + "用户名已经存在"
			},
			password : {
				required : icon + "请输入密码",
				minlength : icon + "密码必须6个字符以上"
			},
			confirm_password : {
				required : icon + "请再次输入密码",
				minlength : icon + "密码必须6个字符以上",
				equalTo : icon + "两次输入的密码不一致"
			},
            mobile : {
                required : icon + "请输入手机号码"
            },
			deptName : icon + "请选择部门",
			email : icon + "请输入您的E-mail",
		}
	})
}

function getParams(){
	var params = {};
	params.username = $("#username").val();
	return JSON.stringify(params);
}

var openDept = function(){
	layer.open({
		type:2,
		title:"选择部门",
		area : [ '300px', '450px' ],
		content:"../../../templates/system/user/deptTree1.html"
	})
}
function loadDept( deptId,deptName){
	$("#deptId").val(deptId);
	$("#deptName").val(deptName);
}


function  selectAreaPopWin(){
    var contentUrl=SERVER_URL+"templates/popwin/areaspopwin.html";
    layer.open({
        type:2,
        title:"选择区域",
        area : [ '500px', '450px' ],
        content:contentUrl,
        success: function(layero, index){
            var body = layer.getChildFrame('body',index);//建立父子联系
            var iframeWin = window[layero.find('iframe')[0]['name']];
            iframeWin.getTreeData(function (earas) {

                var areaIds = '';
                var areaNames = '';
                if(earas){
                    $.each(earas,function (ai,eara) {
                        if(eara.id!=-1){//排除手动创建的根节点
                            if(areaIds!=''){
                                areaIds += ',';
                                areaNames += ',';
                            }
                            areaIds += eara.id;
                            areaNames += eara.text;
                        }
                    });
                }
                $('#areaIds').val(areaIds);
                $('#areaNames').val(areaNames);
                layer.close(index);
            },$('#areaIds').val());
        }
    })
}